# -*- coding: utf-8 -*-
import unittest


class Whatever(unittest.TestCase):
    @unittest.skip('test')  # 微信打卡
    def test005(self):
        import random
        from pathlib import Path
        import codecs
        import itchat
        print(self)

        def send_room_msg(chat, name, msg):
            to_user_name = chat.search_chatrooms(name=name)[0]['UserName']
            chat.send(msg, toUserName=to_user_name)

        # def send_user_msg(chat, name, msg):
        #     to_user_name = chat.search_friends(name=name)[0]['UserName']
        #     chat.send(msg, toUserName=to_user_name)

        def get_one_msg():
            msg_list = []
            for e in Path('./data/book').glob('*.txt'):
                with codecs.open(e, encoding='utf-8') as fd:
                    msg_list.extend(map(lambda x: '#'.join([e.stem, x]), fd.readlines()))
            return random.choice(msg_list)

        message = get_one_msg()
        print(message)
        itchat.auto_login(hotReload=True)
        send_room_msg(itchat, '欢乐西安', message)
        print('打卡成功')

    @unittest.skip('test')  # excel背景色画图
    def test_004(self):
        from openpyxl.styles import PatternFill
        from openpyxl.utils import get_column_letter
        from openpyxl import Workbook
        from PIL import Image
        print(self)

        def get_color_str(rgb):
            r, g, b = rgb[:3]
            return '%02x%02x%02x' % (r, g, b)

        def get_letter(c, r):
            return '%s%d' % (get_column_letter(c + 1), r + 1)

        workbook = Workbook()
        sheet = workbook.active

        im = Image.open(r'./data/family.png')  # .convert('L')
        w, h = im.size

        # 设置行宽列宽
        for x in range(w):
            sheet.column_dimensions[get_column_letter(x + 1)].width = 1
        for y in range(h):
            sheet.row_dimensions[y + 1].height = 5

        # 依次设置每个单元格的背景色
        for y in range(h):
            for x in range(w):
                sheet[get_letter(x, y)].fill = PatternFill('solid', fgColor=get_color_str(im.getpixel((x, y))))

        workbook.save('./data/ascii.xlsx')

    @unittest.skip('ok')  # 点阵字体
    def test_003(self):
        print(self)
        buf_w, buf_h = 16 * 10, 16 * 2
        buffer = [['' for _x in range(buf_w)] for _y in range(buf_h)]

        def show(buf):
            print()
            for y in range(buf_h):
                for x in range(buf_w):
                    print(buf[y][x], end='')
                print()

        def set_pixel(buf, x, y, v):
            buf[y][x] = '..' if v else '  '

        def text_one_char(buf, data, x, y):
            for i, e in enumerate(data):
                xs = x + i % 2 * 8
                ys = y + i // 2
                for dx in range(0, 8):
                    set_pixel(buf, xs + dx, ys, (e & (1 << dx)) != 0)

        def text(buf, s, x, y):
            for i, e in enumerate(s):
                text_one_char(buf, e, x + i * 16, y)

        font_data = [
            [0x80, 0x00, 0x9F, 0x10, 0x90, 0x10, 0x90, 0x08, 0x90, 0x04, 0x9E, 0x02, 0x82, 0x00, 0xE2, 0x7F,
             0x82, 0x02, 0x9E, 0x04, 0x90, 0x04, 0x90, 0x08, 0x90, 0x10, 0x90, 0x22, 0x8A, 0x41, 0x84, 0x00],

            [0x10, 0x02, 0x10, 0x02, 0xD0, 0x3F, 0x08, 0x02, 0x08, 0x02, 0x0C, 0x02, 0xFC, 0x7F, 0x0A, 0x00,
             0x09, 0x02, 0x08, 0x02, 0xC8, 0x3F, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0xF8, 0x7F, 0x08, 0x00],

            [0x00, 0x10, 0x80, 0x3F, 0x7E, 0x08, 0x44, 0x08, 0x88, 0x04, 0xFE, 0x7F, 0x42, 0x40, 0x41, 0x20,
             0xFE, 0x1F, 0x20, 0x00, 0xE0, 0x0F, 0x50, 0x08, 0x88, 0x04, 0x04, 0x03, 0xC2, 0x0C, 0x38, 0x70],

            [0x00, 0x00, 0xFC, 0x1F, 0x84, 0x10, 0x84, 0x10, 0x84, 0x10, 0x84, 0x10, 0x84, 0x10, 0xFC, 0x1F,
             0x84, 0x10, 0x84, 0x10, 0x84, 0x10, 0x84, 0x10, 0x84, 0x10, 0xFC, 0x1F, 0x04, 0x10, 0x00, 0x00],

            [0x00, 0x00, 0xFF, 0x03, 0x00, 0x02, 0x00, 0x22, 0x00, 0x12, 0x00, 0x0A, 0x00, 0x06, 0x00, 0x0A,
             0x00, 0x12, 0x00, 0x22, 0x00, 0x04, 0x00, 0x04, 0x00, 0x48, 0x00, 0x50, 0x00, 0x60, 0x00, 0x40],

            [0x20, 0x02, 0xFF, 0x7F, 0x20, 0x02, 0x20, 0x02, 0xE8, 0x0B, 0x08, 0x48, 0x0F, 0x38, 0xE8, 0x0B,
             0x28, 0x0A, 0x2C, 0x4A, 0xEB, 0x4B, 0x08, 0x70, 0x00, 0x00, 0x12, 0x11, 0x22, 0x22, 0x21, 0x22]
        ]

        text(buffer, font_data, 10, 10)
        show(buffer)

    @unittest.skip(u'测试跳过')  # lrc歌词调整
    def test_002(self):
        print(self)
        import codecs
        new_file = codecs.open(r'./data/秋水伊人 - 朱妍.lrc', 'w', 'utf-8')
        with codecs.open(r'./data/秋水伊人 - 朱妍_17.lrc', 'r', 'gbk') as fd:
            for line in fd:
                # print(line)
                m, s = int(line[1:3]), int(line[4:6])
                t = 60 * m + s - 17 - 5
                m, s = t // 60, t % 60
                new_line = '[%02d:%02d' % (m, s) + line[6:]
                print(new_line)
                new_file.write(new_line)
        new_file.close()

    @unittest.skip(u'skip')  # PySide
    def test_001(self):
        import platform
        major, minor, _ = platform.python_version_tuple()
        print(major, minor)
        # if not (major == '2' and minor == '7'):
        #     return
        #
        # import sys
        # from PySide.QtGui import QWidget
        # from PySide.QtGui import QApplication
        #
        # class Demo(QWidget):
        #     def __init__(self):
        #         super(Demo, self).__init__()
        #
        # app = QApplication(sys.argv)
        # demo = Demo()
        # demo.show()
        # app.exec_()

    @unittest.skip(u'测试')
    def test_000(self):
        print('hello, world!')

    def setUp(self):
        pass

    def tearDown(self):
        pass


if __name__ == '__main__':
    unittest.main()
